import java.awt.Container;
import java.io.*;
import java.net.*;
//import java.io.*;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import org.jsoup.Jsoup;

public class Lyrics implements Runnable {
    
    public void run() {
        try {
            getLyrics(content.content[box]);
        } catch (Exception e) {
        }
    }
    
    public Lyrics(String Song, String Artist, ContentSelector CS, int Box){
        song = Song;
        artist = Artist;
        content = CS;
        box = Box;
    }
    
    public String getArtist() {
        return artist;
    }
    
    public String getSong() {
        return song;
    }
    
    public void setArtist(String artist) {
        this.artist = artist;
    }
    
    public void setSong(String song) {
        this.song = song;
    }

    // gets lyrics from azlyrics.com using a combination of strings to navigate through its directory,
    // creates a file with the lyrics and then reads from that file to display them on a JTextArea
    public void getLyrics(JTextArea lyrics) throws Exception{

        String artist = this.artist.replaceAll("[ ',.-]", "").replace("The", "").toLowerCase();
        String song = this.song.replaceAll("[ ',.-]", "").toLowerCase();
//        System.out.println(artist);
//        System.out.println(song);
        char[] song1 = this.song.toCharArray();

        for(int i=0; i<this.song.length(); i++){
            if(i == 0)
                song1[i] = Character.toUpperCase(song1[i]);
            else if(song1[i] == '.'){
                if((i+1) == this.song.length()){}
                else    song1[i+1] = Character.toUpperCase(song1[i+1]);
            }
            else if((song1[i] == ' ') || (song1[i] == '.'))
                song1[i+1] = Character.toUpperCase(song1[i+1]);
        }
        this.song = new String(song1);

        try {
            URL url = new URL("http://www.azlyrics.com/lyrics/"+artist+"/"+song+".html");
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuilder sb = new StringBuilder();
            String line, text, text2, win = "";
            boolean songStart = false;

            while((line = reader.readLine()) != null){
                if(line.contains("// -->")){}
                else if(line.contains("adding these lyrics.")){}
                else if(line.contains("correcting these lyrics.")){}
                else if(line.contains("LYRICS")){}
                else if(line.contains("MP3")){}
                else if(line.contains("Email")){}
                else if(line.contains("Print")){}
                else if(line.contains("Send \""+this.song+"\" Ringtone to your Cell")){}
                else if(line.contains("ArtistName")){}
                else if(line.contains("SongName")){}
                else if(line.contains("var")){}
                else if(line.contains("services_custom:")){}
                else if(line.contains("\"Amazon\"")){}
                else if(line.contains("url:")){}
                else if(line.contains("icon:")){}
                else if(line.contains("}")){}
                else if(line.contains("Submit Corrections")){}
                else if(line.contains("AZLyrics.com")){}
                else if(line.contains("DMCA Policy")){}
                else if(line.contains("curdate")){}
                else if(line.contains("document.write")){}
                else if(line.contains("try {")){}
                else if(line.contains("pageTracker")){}
                else if(line.contains("\""+this.song+"\" lyrics")){}
                else if(line.contains("\""+this.song+"\"")){
                    songStart = true;
                    sb.append(line);
                    if(songStart == false){
                        text = Jsoup.parse(sb.toString()).text();
                        if(text.isEmpty() == true){}
                        else{
                            text2 = text.replace("’", "'").trim();
                            win = win.concat(text2).concat("\n");
                        }
                    }
                    else{
                        text = Jsoup.parse(sb.toString()).text();
                        text2 = text.replace("’", "'").trim();
                        win = win.concat(text2).concat("\n");
                    }
                    sb.delete(0, sb.length());
                }
                else{
                    if(songStart == false){
                        sb.append(line);
                        if(songStart == false){
                            text = Jsoup.parse(sb.toString()).text();
                            if(text.isEmpty() == true){}
                            else{
                                text2 = text.replace("’", "'").trim();
                                win = win.concat(text2).concat("\n");
                            }
                        }
                        else{
                            text = Jsoup.parse(sb.toString()).text();
                            text2 = text.replace("’", "'").trim();
                            win = win.concat(text2).concat("\n");
                        }
                        sb.delete(0, sb.length());
                    }
                    else{
                        sb.append(line);
                        if(songStart == false){
                            text = Jsoup.parse(sb.toString()).text();
                            if(text.isEmpty() == true){}
                            else{
                                text2 = text.replace("’", "'").trim();
                                win = win.concat(text2).concat("\n");
                            }
                        }
                        else{
                            text = Jsoup.parse(sb.toString()).text();
                            text2 = text.replace("’", "'").trim();
                            win = win.concat(text2).concat("\n");
                        }
                        sb.delete(0, sb.length());
                    }
                }
            }

            //System.out.print(win);
            lyrics.setText(win);

        } catch (MalformedURLException e) {
            System.out.println("Invalid URL");

        } catch (IOException e) {
            System.out.println("Song requested isn't in the database or was misspelled");
        }

    }

    /* public void testLyrics() throws Exception{

        JFrame frame = new JFrame("Lyrics");
        Container pane = frame.getContentPane();
        JTextArea lyrics = new JTextArea(5, 30);
        pane.add(lyrics);
        frame.pack();
        frame.setVisible(true);
        Lyrics lyr = new Lyrics("s.o.s.", "Jonas Brothers");

        lyr.getLyrics(lyrics);
    } */

    private String song, artist;
    private ContentSelector content;
    private int box;
        
}